package tests.create;

import static org.junit.Assert.*;

import java.net.UnknownHostException;

import model.Budget;
import model.BudgetFactory;
import model.Note;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

import cc.test.bridge.NoteInterface;

public class AddNoteTest00
{
	Budget budget;
	BudgetFactory factory;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception
	{
	}

	@Before
	public void setUp() throws Exception
	{
		factory = new BudgetFactory();
		budget = (Budget) factory.create();
        budget.setName("Test budget");
        budget.setDescription("This is a test budget.");
        //budget.commit();
	}

	@After
	public void tearDown() throws Exception
	{
		Mongo mongo = null;
  		try
  		{
  			mongo = new Mongo();
  		} catch (UnknownHostException e)
  		{
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		} catch (MongoException e)
  		{
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		}
  		
          DB db = mongo.getDB("cc");
          DBCollection collection = db.getCollection(budget.getRepositoryName());
          collection.remove(new BasicDBObject("entry.id", budget.getId()));
          
//		Helper.delete(budget, WorkletContext.getInstance());
	}

	@Test
	public void test()
	{
		
		
		 try
		 {
			 Note note = (Note) budget.createNote();
			 note.setText("This is a note");
			 budget.add(note);
		
			 Boolean successful = budget.commit();
			if(!successful){
				fail("Could not create/add note");
			}
         }
		 catch(Exception e) {
			 e.printStackTrace();
			 fail("Add note failed with exception " + e);
		 }
	}

}
